package com;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class ScheduleService {
    private List<Schedule> scheduleList;
    private LogService logService;
    public ScheduleService(List<Schedule> scheduleList, LogService logService) {
        this.scheduleList = scheduleList;
        this.logService = logService;
                                                                               }
    public boolean addSchedule(Schedule schedule, User operator) {
        for (Schedule existing : scheduleList) {
            if (existing.getHall().getHallId().equals(schedule.getHall().getHallId()) &&
                    existing.getShowTime().equals(schedule.getShowTime())) {
                System.out.println("错误：该放映厅此时已有场次");
                return false;                                              }
                                                }
        scheduleList.add(schedule);
        logService.recordLog("添加场次", operator,
                "为影片《" + schedule.getMovie().getTitle() + "》添加" +
                        schedule.getShowTime() + "场次");
        return true;                                             }
    public boolean deleteSchedule(String scheduleId, User operator) {
        for (Schedule s : scheduleList) {
            if (s.getScheduleId().equals(scheduleId)) {
                if (s.hasSoldTickets()) {
                    System.out.println("错误：该场次已售出票，无法删除");
                    return false;       }
                scheduleList.remove(s);
                logService.recordLog("删除场次", operator,
                        "删除" + s.getMovie().getTitle() + "的" +
                                s.getShowTime() + "场次");
                return true;                          }
                                        }
        System.out.println("错误：场次不存在");
        return false;                                              }
    public boolean updateSchedule(Schedule updatedSchedule, User operator) {
        for (int i = 0; i < scheduleList.size(); i++) {
            if (scheduleList.get(i).getScheduleId().equals(updatedSchedule.getScheduleId())) {
                if (scheduleList.get(i).hasSoldTickets() &&
                        (!scheduleList.get(i).getShowTime().equals(updatedSchedule.getShowTime()) ||
                                !scheduleList.get(i).getHall().getHallId().equals(updatedSchedule.getHall().getHallId()))) {
                    System.out.println("错误：该场次已售出票，无法修改时间或放映厅");
                    return false;
                }
                scheduleList.set(i, updatedSchedule);
                logService.recordLog("更新场次", operator,
                        "更新" + updatedSchedule.getMovie().getTitle() + "的" +
                                updatedSchedule.getShowTime() + "场次");
                return true;}
                                                       }
        System.out.println("错误：场次不存在");
        return false;                                                     }
    public Schedule getScheduleById(String scheduleId) {
        for (Schedule s : scheduleList) {
            if (s.getScheduleId().equals(scheduleId)) {
                return s;                             }
                                        }
        return null;                                   }
    public List<Schedule> getSchedulesByMovieId(String movieId) {
        List<Schedule> result = new ArrayList<>();
        for (Schedule s : scheduleList) {
            if (s.getMovie().getMovieId().equals(movieId)) {
                result.add(s);                             }
                                        }
        return result;
    }
    public List<Schedule> getSchedulesInDays(int days) {
        List<Schedule> result = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime endTime = now.plusDays(days);
        for (Schedule s : scheduleList) {
            if (s.getShowTime().isAfter(now) && s.getShowTime().isBefore(endTime)) {
                result.add(s);                                                     }
                                        }
        return result;                                  }
    public List<Schedule> autoArrangeSchedules(List<Movie> movies, List<Hall> halls,
                                               LocalDateTime startDate, int days) {
        List<Schedule> newSchedules = new ArrayList<>();
        for (int day = 0; day < days; day++) {
            LocalDateTime currentDate = startDate.plusDays(day);
            for (Hall hall : halls) {
                LocalDateTime showTime = currentDate.withHour(10);
                for (Movie movie : movies) {
                    String scheduleId = "S" + System.currentTimeMillis() + "_" + hall.getHallId();
                    List<Seat> seats = new ArrayList<>();
                    for (int row = 1; row <= hall.getRowCount(); row++) {
                        for (int col = 1; col <= hall.getColCount(); col++) {
                            seats.add(new Seat(hall.getHallId() + "-" + row + "排" + col + "号", row, col));
                        }
                    }
                    double price = hall.getHallId().contains("黄金") ? 80.0 :
                            hall.getHallId().contains("钻石") ? 100.0 : 60.0;
                    Schedule schedule = new Schedule(scheduleId, movie, hall, showTime, price, seats);

                    newSchedules.add(schedule);



                    if (showTime.getHour() >= 23) break;
                }}}
        for (Schedule s : newSchedules) {
            scheduleList.add(s);        }

        return newSchedules;}}